
//
//  MTLabel.m
//  MoveLabel
//
//  Created by 常兴宇 on 13-10-8.
//  Copyright (c) 2013年 常兴宇. All rights reserved.
//

#import "MTLabel.h"

#define BASETIME 40
@implementation MTLabel

-(void)dealloc{
    [label release];
    [mLabel release];
    [self.textColor release];
    [self.font release];
    self.text = nil;
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self load];
    }
    return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self load];
    }
    return self;
}
-(void)load{
    self.space = 20;
    scrollState = 0;
//    self.font = [UIFont systemFontOfSize:17.0f];
    self.clipsToBounds = YES;
}
-(void)setText:(NSString *)text{
    
    if (_text != text) {
        [_text release];
        _text = nil;
        _text = [text copy];
    }
//    NSLog(@"_text:%@",_text);
    size = [_text sizeWithFont:self.font constrainedToSize:CGSizeMake(MAXFLOAT,self.bounds.size.height) lineBreakMode:NSLineBreakByCharWrapping];
    if (self.textAlignment == NSTextAlignmentCenter) {
        if (size.width < self.bounds.size.width) {
            float labelx = (self.bounds.size.width - size.width) / 2;
            if (!label) {
                label = [[UILabel alloc]initWithFrame:CGRectMake(labelx, 0, size.width, self.bounds.size.height)];
//                NSLog(@"lable:%@",label);
            }
        }else{
            if (!label) {
                label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, size.width, self.bounds.size.height)];

            }

        }
    }else{
        if (!label) {
            label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, size.width, self.bounds.size.height)];
        }

        
    }
    

    label.textColor = self.textColor;
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    label.text = _text;
    label.font = self.font;
    label.lineBreakMode = NSLineBreakByCharWrapping;
    [self addSubview:label];
}
-(void)startScroll{
    switch (self.scrollType) {
        case ScrollCirculation:
        {
//            NSLog(@"242342");
            timer = [NSTimer timerWithTimeInterval:size.width / BASETIME target:self selector:@selector(scrollCirculation) userInfo:nil repeats:YES];
        }
            break;
        case ScrollSingle:
        {
            [mLabel removeFromSuperview];
            if (size.width > self.bounds.size.width) {
                if (!mLabel) {
                    mLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.bounds.size.width - 100, 0, size.width, self.bounds.size.height)];
                }
                mLabel.font = label.font;
                mLabel.text = label.text;
                mLabel.textColor = label.textColor;
                mLabel.textAlignment = label.textAlignment;
                mLabel.lineBreakMode = label.lineBreakMode;
                mLabel.backgroundColor = label.backgroundColor;
                [self addSubview:mLabel];
            }
            
            timer = [NSTimer timerWithTimeInterval:size.width / BASETIME target:self selector:@selector(scrollSingle) userInfo:nil repeats:YES];
        }
            break;
        default:
            break;
    }
//    NSLog(@"contentOffSet:%f   %f",size.width, self.bounds.size.width);

    if (size.width > self.bounds.size.width) {
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
        [timer fire];
    }

}

-(void)scrollCirculation{
    scrollState ++;
    if (scrollState % 2 == 1) {
        [UIView animateWithDuration:size.width /  BASETIME delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
            label.frame = CGRectMake(-size.width + self.bounds.size.width - 20, 0, size.width, self.bounds.size.height);
        } completion:nil];
    }else{
        [UIView animateWithDuration:size.width /  BASETIME delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
            label.frame = CGRectMake(20, 0, size.width, self.bounds.size.height);
        } completion:nil];

    }
    
}

-(void)scrollSingle{
    
    label.frame = CGRectMake(0, 0, size.width, self.bounds.size.height);
    mLabel.frame = CGRectMake(size.width + self.space, 0, size.width, self.bounds.size.height);

    [UIView animateWithDuration:(size.width / BASETIME) - 0.3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        label.frame = CGRectMake(-size.width - self.space, 0, size.width, self.bounds.size.height);
        mLabel.frame = CGRectMake(0, 0, size.width, self.bounds.size.height);
    
    } completion:^(BOOL finished) {
    }];

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
